package com.boc.ewr.uniapp.api.service.impl;

import com.alipay.api.domain.Car;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.boc.ewr.bcimp.entity.CaRtnList;
import com.boc.ewr.bcimp.entity.Cac;
import com.boc.ewr.bcimp.entity.CusmTagList;
import com.boc.ewr.bcimp.service.ICaRtnListService;
import com.boc.ewr.bcimp.service.ICacService;
import com.boc.ewr.bcimp.service.ICusmTagListService;
import com.boc.ewr.business.entity.*;
import com.boc.ewr.business.service.IAcctDtlClassA1Service;
import com.boc.ewr.business.service.IAcctDtlClassA2Service;
import com.boc.ewr.business.service.IAcctDtlClassA3Service;
import com.boc.ewr.business.service.ICusmTagRelationService;
import com.boc.ewr.clkIn.entity.CaDtdClkin;
import com.boc.ewr.clkIn.service.ICaDtdClkinService;
import com.boc.ewr.uniapp.api.service.IBusCusmInfoApiService;
import com.boc.ewr.uniapp.api.vo.AcctInfoVo;
import com.boc.ewr.uniapp.api.vo.CaRecordVo;
import com.boc.ewr.uniapp.api.vo.TagReqVo;
import com.boc.ewr.work.entity.WorkRecordDtl;
import com.boc.ewr.work.service.IWorkRecordDtlService;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.pegdown.ParsingTimeoutException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author : Wzz
 * @create : 2023-05- 31- 11:12
 * @desc :
 */
@Service
public class BusCusmInfoApiServiceImpl implements IBusCusmInfoApiService {

    @Autowired
    private ICusmTagListService cusmTagListService;

    @Autowired
    private ICusmTagRelationService cusmTagRelationService;

    @Autowired
    private ICacService cacService;

    @Autowired
    private IWorkRecordDtlService workRecordDtlService;

    @Autowired
    private ICaDtdClkinService caDtdClkinService;

    @Autowired
    private ICaRtnListService caRtnListService;

    @Override
    public Map<String, List<CusmTagList>> queryTageByCusmNm(String cusmNm) {
        return cusmTagRelationService.queryTageByCusmNm(cusmNm);
    }

    @Override
    public void updateTagByCusmNm(String cCusmNm) {
        cusmTagRelationService.updateTagCusmNm(cCusmNm);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addOrgDiyTag(TagReqVo tagReqVo) {
        CusmTagList cusmTagList = new CusmTagList();
        BeanUtils.copyProperties(tagReqVo, cusmTagList);

        // 添加创建机构号
        cusmTagList.setCreateOrgNo(this.getLoginUser().getOrgCode());
        cusmTagListService.save(cusmTagList);

        //添加对应关系
        CusmTagRelation cusmTagRelation = new CusmTagRelation();
        cusmTagRelation.setBusCusmInfoId(tagReqVo.getBusAcctId());
        cusmTagRelation.setCusmTagId(cusmTagList.getId());

        cusmTagRelationService.save(cusmTagRelation);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeOrgDiyTag(TagReqVo tagReqVo) {
        cusmTagListService.removeById(tagReqVo.getId());

        cusmTagRelationService.remove(
                new LambdaQueryWrapper<CusmTagRelation>()
                        .eq(CusmTagRelation::getBusCusmInfoId, tagReqVo.getBusAcctId())
                        .eq(CusmTagRelation::getCusmTagId, tagReqVo.getId())
        );
    }

    @Override
    public List<AcctInfoVo> getAcctInfoByCusmName(String cusmNm) {
        List<Cac> cacList = cacService.list(
                new LambdaQueryWrapper<Cac>()
                        .eq(Cac::getCusmName, cusmNm)
        );
        List<AcctInfoVo> resultList = new ArrayList<>();
        for (Cac cac : cacList) {
            AcctInfoVo acctInfoVo = new AcctInfoVo();
            BeanUtils.copyProperties(cac, acctInfoVo);

            resultList.add(acctInfoVo);
        }

        return resultList;
    }

    @Override
    public WorkRecordDtl getLatestCallByCusmNm(String cusmNm) {
        List<WorkRecordDtl> list = workRecordDtlService.list(
                new LambdaQueryWrapper<WorkRecordDtl>()
                        .eq(WorkRecordDtl::getCusmNm, cusmNm)
                        .orderByDesc(WorkRecordDtl::getCommunicateTime)
        );

        return list.size() > 0 ? list.get(0) : null;

    }

    @Override
    public CaDtdClkin getLatestDtdPhoto(String cusmNm) {
        List<CaDtdClkin> list = caDtdClkinService.list(
                new LambdaQueryWrapper<CaDtdClkin>()
                        .eq(CaDtdClkin::getCusmNm, cusmNm)
                        .orderByDesc(CaDtdClkin::getClkinTime)
        );
        return list.size() > 0 ? list.get(0) : null;
    }

    @Override
    public CaRecordVo getCaRecordByCusmNm(String cusmNm) {
        List<CaRtnList> list = caRtnListService.list(
                new LambdaQueryWrapper<CaRtnList>()
                        .eq(CaRtnList::getCusmNm, cusmNm)
                        .orderByDesc(CaRtnList::getRtnSignDate)
        );
        if (list.size() > 0) {
            CaRtnList caRtnList = list.get(0);
            CaRecordVo caRecordVo = new CaRecordVo();
            BeanUtils.copyProperties(caRtnList, caRecordVo);
            return caRecordVo;
        }
        return null;
    }


    private LoginUser getLoginUser() {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        return sysUser;
    }
}
